package com.mobileserver.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONStringer;

import com.mobileserver.dao.BookClassDAO;
import com.mobileserver.domain.BookClass;
import com.mobileserver.util.DBUtil;
 
public class BookClassServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BookClassServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String action = request.getParameter("action");
		if(action.equals("query")) {
			BookClassDAO bookClassDAO = new BookClassDAO();
			List<BookClass> bookClassList = bookClassDAO.QueryAllBookClass();
			
			//输出xml格式的数据
			/*<?xml version="1.0" encoding="utf-8"?>
			 *<BookClasss>
			 *	<BookClass>
			 *		<bookClassId>1</bookClassId>
			 *		<bookClassName>计算机类</bookClassName>
			 *	</BookClass> 
			 *	<BookClass>
			 *		<bookClassId>2</bookClassId>
			 *		<bookClassName>历史类</bookClassName>
			 *	</BookClass>
			 *</BookClasss>
			 */
			StringBuffer sb = new StringBuffer();
			sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>").append("\r\n").append("<BookClasss>").append("\r\n");
			for(int i = 0; i<bookClassList.size(); i++) {
				sb.append("	<BookClass>").append("\r\n")
					.append("		<bookClassId>").append(bookClassList.get(i).getBookClassId()).append("</bookClassId>").append("\r\n")
					.append("		<bookClassName>").append(bookClassList.get(i).getBookClassName()).append("</bookClassName>").append("\r\n")
				.append("	</BookClass>").append("\r\n");
			}
			sb.append("</BookClasss>").append("\r\n");
		 
			response.setCharacterEncoding("utf-8");
			PrintWriter out = response.getWriter();
			out.print(sb.toString());
		} else if(action.equals("add")) {
			String bookClassName=new String(request.getParameter("bookClassName").getBytes("iso-8859-1"),"UTF-8");  
			BookClass bookClass = new BookClass();
			bookClass.setBookClassName(bookClassName);
			BookClassDAO bookClassDAO = new BookClassDAO();
			String result = bookClassDAO.AddBookClass(bookClass);
			response.setCharacterEncoding("utf-8");
			PrintWriter out = response.getWriter();
			out.print(result);
		} else if(action.equals("delete")) {
			String bookClassId = new String(request.getParameter("bookClassId").getBytes("iso-8859-1"),"UTF-8");
			BookClassDAO bookClassDAO = new BookClassDAO();
			String result = bookClassDAO.DeleteBookClass(bookClassId);
			response.setCharacterEncoding("utf-8");
			PrintWriter out = response.getWriter();
			out.print(result);
		} else if(action.equals("updateQuery")) {
			int bookClassId = Integer.parseInt(request.getParameter("bookClassId"));
			BookClassDAO bookClassDAO = new BookClassDAO();
			BookClass bookClass = bookClassDAO.GetBookClass(bookClassId);
			//客户端查询对象信息，返回json数据格式
			//将List<Person>组织成JSON字符串   
	        JSONStringer stringer = new JSONStringer();  
	        try{  
	            stringer.array();   
	            stringer.object();
	            stringer.key("bookClassId").value(bookClass.getBookClassId());
	            stringer.key("bookClassName").value(bookClass.getBookClassName());
	            stringer.endObject();
	            stringer.endArray();  
	        }  
	        catch(Exception e){}  
	        response.getOutputStream().write(stringer.toString().getBytes("UTF-8"));  
	        response.setContentType("text/json; charset=UTF-8");  //JSON的类型为text/json
			
		} else if(action.equals("update")) {
			//真正执行更新业务
			String bookClassId = new String(request.getParameter("bookClassId").getBytes("iso-8859-1"),"UTF-8");
			String bookClassName = new String(request.getParameter("bookClassName").getBytes("iso-8859-1"),"UTF-8");
			BookClass bookClass = new BookClass();
			bookClass.setBookClassId(Integer.parseInt(bookClassId));
			bookClass.setBookClassName(bookClassName);
			BookClassDAO bookClassDAO =new BookClassDAO();
			String result = bookClassDAO.UpdateBookClass(bookClass);
			response.setCharacterEncoding("utf-8");
			PrintWriter out = response.getWriter();
			out.print(result);
		} else if(action.equals("GetBookClassName")) {
			int bookClassId = Integer.parseInt(request.getParameter("bookClassId"));
			BookClassDAO bookClassDAO = new BookClassDAO();
			BookClass bookClass = bookClassDAO.GetBookClass(bookClassId);  
			response.setCharacterEncoding("UTF-8");
			response.setHeader("Content-Type", "text/plain;charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.print(bookClass.getBookClassName());
		}
		
	}

}
